﻿using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Sqlite.Repositories
{
    public class DataRepository : RepositoryBase, IDataRepository
    {
        public int Delete(Data entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
            return db.SaveChanges();
        }

        public Data Get(int id)
        {
            return db.Datas.Find(id);
        }

        public List<Data> GetAll()
        {
            return db.Datas.ToList();
        }

        public List<Data> GetAll(string keyword)
        {
            return db.Datas.ToList().Where(t => t.CarName.Contains(keyword)).ToList();
        }

        public List<Data> GetAllTemp()
        {
            // 条件： 毛重日期或者皮重日期任一个为空
            return db.Datas.ToList()
                .Where(t => t.Mdate == null ||t.Pdate == null)
                .OrderByDescending(t => t.Id)
                .ToList();
        }

        public List<Data> GetAllToday()
        {
            // 条件: 毛重日期、皮重日期都不为空的当天数据
            return db.Datas.ToList()
                .Where(t => t.InsertDate.Date == DateTime.Now.Date && t.Mdate!= null && t.Pdate != null)
                .OrderByDescending(t => t.Id)
                .ToList();
        }

        public int Insert(Data entity)
        {
            db.Datas.Add(entity);
            return db.SaveChanges();
        }

        public Data Select(string keyword)
        {
            return db.Datas.ToList().Find(t => t.CarName == keyword);
        }

        public int Update(Data entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
